<?php

namespace App\Store\Controllers\Message;

use App\Admin\Models\SiteCompany;
use App\Http\Controllers\Controller;
use App\Store\Models\Message\Rule;
use App\Store\Services\Message\Lianlu;
use Illuminate\Http\Request;
use App\Store\Validate\Message\Rule as ValidateMessageRule;
use App\Common\Services\Store\Common as StoreCommon;

class RuleController extends Controller
{
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_ids', 'type', 'content', 'date']);
        $Validate = new ValidateMessageRule('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $store_list = $req_data['store_ids'];
        $store_info_data = is_array($store_list) ? $store_list : (is_array(json_decode($store_list, true)) ? json_decode($store_list, true) : []);

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $data = [];
        foreach ($store_info_data as $k => $v) {
            $data[$k]['cp_id'] = $cp_id;
            $data[$k]['store_id'] = $v;
            $data[$k]['type'] = $req_data['type'];
            $data[$k]['content'] = $req_data['content'];
            $data[$k]['date'] = !empty($req_data['date']) ? $req_data['date'] : '1970-01-01';
            $data[$k]['create_at'] = time();
            $data[$k]['create_account_id'] = $accountinfo['id'];;
            $data[$k]['create_account_real_name'] = $accountinfo['real_name'];
        }
        Rule::insert($data);
        return response()->json(['code' => '000', 'msg' => '添加成功']);
    }

    public function getlist(Request $request)
    {
        $list = Rule::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateMessageRule('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        Rule::where('id', $req_data['id'])->update([
            'is_del' => 2,
            'update_at' => time(),
            'update_account_id' => $accountinfo['id'],
            'update_account_real_name' => $accountinfo['real_name'],
        ]);
        return response()->json(['code' => '000', 'msg' => '删除成功']);
    }

    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'store_id', 'type', 'content', 'date', 'status']);
        $Validate = new ValidateMessageRule('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        Rule::where('id', $req_data['id'])->update([
            'store_id' => $req_data['store_id'],
            'type' => $req_data['type'],
            'content' => $req_data['content'],
            'date' => !empty($req_data['date']) ? $req_data['date'] : '1970-01-01',
            'status' => $req_data['status'],
            'update_at' => time(),
            'update_account_id' => $accountinfo['id'],
            'update_account_real_name' => $accountinfo['real_name'],
        ]);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }
}

